package com.sumainfor.pc.dev.service.impl;

import com.sumainfor.common.Constants;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.ConvertDateTime;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.dev.dao.MovableMapper;
import com.sumainfor.pc.dev.dao.UtilsMapper;
import com.sumainfor.pc.dev.service.MovableHousingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 功能描述:
 *
 * @Auther: zhlu
 * @Date: 2019/3/10 14:05
 * @Description:
 */
@Service
public class MovableService implements Serializable {
    private ReentrantLock lock = new ReentrantLock(false);
    Logger log = LoggerFactory.getLogger(MovableService.class);

    @Autowired
    MovableMapper MovableMapper;

    @Autowired
    UtilsMapper utilsMapper;

    @Autowired
    MovableHousingService movableHousingService;


    /**
     * 功能描述: 
     * 获取首页活动列表
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/10 14:07
     */
    public List<Map<String,Object>> getMovableList(Map<String,Object>params){
        List<Map<String,Object>> getMovableList=MovableMapper.getMovableList(params);
        return getMovableList;
    }

    /**
     * 功能描述: 
     * 
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/10 14:08
     */
    public Integer getMovableListCount(Map<String,Object>params){
        Integer getMovableListCount=MovableMapper.getMovableListCount(params);
        return getMovableListCount;
    }

    /**
     * 功能描述: 
     * 根据编号获取首页活动详情
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/10 15:19
     */
    public Map<String,Object>getMovableMap(Map<String,Object>params){
        Map<String,Object>getMovableMap=MovableMapper.getMovableMap(params);
        return getMovableMap;
    }

    /**
     * 功能描述: 
     * 根据活动编号获取活动和房源中间表
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/12 18:01
     */
    public List<Map<String,Object>>getMovableHousingList(Map<String,Object>params){
        List<Map<String,Object>>getMovableHousingList=MovableMapper.getMovableHousingList(params);
        return getMovableHousingList;
    }

    /**
    * <p>
    * 创建活动
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    public JsonResult insertMovable(Map<String,Object> params){
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            log.info("增加操作");
            params.put("term", "7");//创建端  1 租客端 2市场部 3 工程部 4运营部 5财务部 6总经办 7发展部
            params.put("fnc",
                    this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonInsertParms(params);
            String UUID;
            Map<String,Object>uuid=utilsMapper.getUUID(new HashMap<String,Object>(){{
                put("uuid", "MovId");
                put("database", Constants.DATA_SOURCE_TCDP);
                put("surface", "tcdp_movable");
            }});
            if(uuid!=null){
                int number=Integer.valueOf(uuid.get("uuid").toString());
                UUID= ComUtils.getLocalTrmSeqNum("MO",number);
            }else{
                UUID=ComUtils.getLocalTrmSeqNum("MO",0);
            }
            params.put("MovId",UUID);
            params.put("CreationTime", ConvertDateTime.getCurrentTime());
            Integer cout = MovableMapper.insertMovable(params);
            //创建活动与房源的关联表
            List<Map<String,Object>> HouInfoIdList = (List<Map<String, Object>>) params.get("HouInfoIdList");
            for (Map<String,Object> HouInfoIdMap:HouInfoIdList) {
                HouInfoIdMap.put("MovId",params.get("MovId"));
                movableHousingService.insertMobHou(HouInfoIdMap);
            }
            if(cout<=0){
                result.putFailed("创建失败!");
            }else{
                result.putSuccess("创建成功!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事物回滚
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();
        }
        return result;
    }

    /**
    * <p>
    * 编辑活动
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    public JsonResult updateMovable(Map<String,Object> params){
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            log.info("更新操作");
            System.out.println("params"+params);
            //A查询的房源详表
            List<Map<String,Object>> HouInfoIdOneList = (List<Map<String, Object>>) params.get("HouInfoIdOneList");
            System.out.println("查询的房源详表"+HouInfoIdOneList);
            //B传过来的房源详表编号集合
            List<Map<String,Object>> HouInfoIdList = (List<Map<String, Object>>) params.get("HouInfoIdList");
            System.out.println("传过来的房源详表编号集合"+HouInfoIdList);
            if ( params.get("HouInfoIdOneList")!=null||params.get("HouInfoIdList")!=null) {
                if (HouInfoIdOneList.size() > 0 || HouInfoIdList.size() > 0) {
                  for (Map<String,Object> HouInfoIdOne:HouInfoIdOneList) {
                            HouInfoIdOne.put("MovId", params.get("MovId"));
                            movableHousingService.deleteMobHou(HouInfoIdOne);
                    }
                    for (Map<String,Object> HouInfoId:HouInfoIdList) {
                            HouInfoId.put("MovId", params.get("MovId"));
                            movableHousingService.insertMobHou(HouInfoId);
                    }
                }
            }
            params.put("by", ContextUtils.getUserId());//操作人
            params.put("term","7");//1 租客端 2市场部 3 工程部 4运营部 5财务部 6总经办 7发展部
            params.put("fnc",
                    this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonUpdateParms(params);
            if(MovableMapper.updateMovable(params)<=0){
                result.putFailed("修改失败");
            }else{
                result.putSuccess("修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事务回滚
            result.putFailed("服务器繁忙");
        } finally {
            lock.unlock();
        }
        return result;


    }

}
